<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/6/14
 * Time: 13:42
 */

namespace Mytype\Controller;
use Common\Controller\AdminbaseController;
use Org\Util\DataTree;

class AdController extends AdminbaseController
{
    public function index()
    {
        $count      = M('add')->where(['check'=>1])->count();
        $Page       = new \Think\Page($count,10);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();
        $list = M('add')->where(['check'=>1])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }
        /*
        echo '<pre/>';
        print_r($list);
        die();
        */
        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }
    /*待审核*/
    public function check()
    {
        $count      = M('add')->where(['check'=>0])->count();
        $Page       = new \Think\Page($count,10);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();
        $list = M('add')->where(['check'=>0])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }
        /*
        echo '<pre/>';
        print_r($list);
        die();
        */
        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }
    /*广告添加*/
    public function add()
    {
        $tree = new DataTree();
        $data = $tree->tree(M('city')->select(),'name','id');
        $this->assign('city',$data);
        if(IS_POST){
            $post  = i('post.');
            $cityname = M('city')->where(['id'=>$post['type']])->find();
            $data = [
                'title'=>$post['title'],
                'key'=>$post['key'],
                'des'=>$post['des'],
                'time'=>$post['time'],
                'content'=>$post['content'],
                'thumb'=>$post['thumb'],
                'check'=>$post['check'],
                'read'=>$post['read'],
                'city'=>$post['type'],
                'sort'=>$post['sort'],
                'maker'=>$_SESSION['name'],
                'cityname'=>$cityname['name'],
                'login'=>$_SESSION['ADMIN_ID']
            ];
            if(trim(empty($post['thumb']))){
                $this->error('请上传封面图');
            }else{
                if(M('add')->add($data)){
                    $this->success('添加成功');
                }else{
                    $this->error('失败，未知错误');
                }
            }
        }else{
            $this->display();
        }
    }
    /*广告删除*/
    public function del()
    {
        $id = i('get.id');
        if(M('add')->where(['id'=>$id])->delete()){
            $this->success('删除成功');
        }else{
            $this->error('失败，未知错误');
        }
    }
    /*批量删除*/
    public function delall()
    {
        $post = i('post.');
        if(IS_POST) {
            foreach ($post['ids'] as $k) {
                M('add')->where(['id' => $k])->delete();
            }
            $this->redirect(U('Ad/index'));
        }
    }
    /*通过审核*/
    public function pass()
    {
        $id = i('get.id');
        if(M('add')->where(['id'=>$id])->save(['check'=>1])){
            $this->success('成功');
        }else{
            $this->error('失败，未知错误');
        }
    }
    /*修改排序*/
    public function change()
    {
        $id = i('get.id');
        $this->assign('vo',M('add')->where(['id'=>$id])->find());
        $sort = i('post.sort');
        if(IS_POST) {
            if(M('add')->where(['id'=>$id])->save(['sort'=>$sort])){
                $this->success('成功');
            }else{
                $this->error('失败，未知错误');
            }
        }else{
            $this->display();
        }
    }
    /*搜索广告*/
    public function ser()
    {
        $ser = i('post.ser');
        $where= array('like','%'.$ser.'%');
        if(IS_POST){
            if(trim(empty($ser))){
                $this->error('查询内容不能为空');
            }
            $count      = M('add')->where(['title|cityname'=>$where])->count();
            if($count==0){
                $this->error('没有找到你要找的内容,抱歉了。');
            }
            $Page       = new \Think\Page($count,10);
            $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
            $show       = $Page->show();
            $list = M('add')->where(['title|cityname'=>$where])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            foreach ($list as $k=>$v) {
                $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
            }

            $this->assign(['page'=>$show,'data'=>$list]);
            $this->display();
        }else{
            $this->display();
        }
    }
}